<template>
  <view>
  
    <view
      class="address-management"
      :class="addressList.length < 1 && page > 1 ? 'fff' : ''"
    >
      <radio-group
        class="radio-group"
        @change="radioChange"
        v-if="addressList.length"
      >
        <view
          class="item borRadius14"
          v-for="(item, index) in addressList"
          :key="index"
        >
          <view class="address" @click="goOrder(item.id)">
            <view class="consignee"
              >{{ item.realName
              }}<text class="phone">{{ item.phone }}</text></view
            >
            <view
              >{{ item.province }}{{ item.city }}{{ item.district
              }}{{ item.detail }}</view
            >
          </view>
          <view class="operation acea-row row-between-wrapper">
           
            <radio
              class="radio"
              :value="index.toString()"
              :checked="item.isDefault"
            >
              <text :class="{ 'checked-text': item.isDefault }">设为默认</text>
            </radio>
         
           
        
         
           
            <view class="acea-row row-middle">
              <view class="iconfont" @click="setCopy(item)">复制</view>
              <view class="iconfont" @click="editAddress(item.id)">编辑</view>
              <view class="iconfont" @click="delAddress(index)">删除</view>
            </view>
          </view>
        </view>
      </radio-group>
      <view
        class="loadingicon acea-row row-center-wrapper"
        v-if="addressList.length"
      >
        <text
          class="loading iconfont icon-jiazai"
          :hidden="loading == false"
        ></text
        >{{ loadTitle }}
      </view>
      <view class="noCommodity" v-if="addressList.length < 1 && page > 1">
        <view class="pictrue">
          <image src="https://cshy.store/file/sysFile/wechart/noAddress.png"></image>
        </view>
      </view>
      <view style="height: 120rpx"></view>
    </view>
    <view class="footer acea-row row-between-wrapper">
     
   
      <view
        class="addressBnt bg-color"
        :class="'on'"
        @click="addAddress"
        >添加新地址</view
      >
    
  
    </view>
   
    <!-- <home></home> -->
  </view>
</template>

<script>
import {
  getAddressList,
  setAddressDefault,
  delAddress,
  editAddress,
  postAddress,
} from '@/api/user.js'
import { toLogin } from '@/libs/login.js'
import { mapGetters } from 'vuex'
 
import home from '@/components/home'
export default {
  components: {
   
    home,
  },
  data() {
    return {
      addressList: [],
      cartId: '',
      pinkId: 0,
      couponId: 0,
      loading: false,
      loadend: false,
      loadTitle: '加载更多',
      page: 1,
      limit: 20,
      isAuto: false, //没有授权的不会自动授权
      isShowAuth: false, //是否隐藏授权
      bargain: false, //是否是砍价
      combination: false, //是否是拼团
      secKill: false, //是否是秒杀
      activeIcon: 'https://img01.yzcdn.cn/vant/user-active.png',
      inactiveIcon: 'https://img01.yzcdn.cn/vant/user-inactive.png',
      orderType:'',
      pickupCode:''
    }
  },
  computed: mapGetters(['isLogin']),
  watch: {
    isLogin: {
      handler: function (newV, oldV) {
        if (newV) {
          this.getUserAddress(true)
        }
      },
      deep: true,
    },
  },
  onLoad(options) {
    if (this.isLogin) {
      this.preOrderNo = options.preOrderNo || 0
      this.orderType = options.orderType || null
      this.pickupCode = options.pickupCode || null
      // this.pinkId = options.pinkId || 0;
      // this.couponId = options.couponId || 0;
      // this.secKill = options.secKill || false;
      // this.combination = options.combination || false;
      // this.bargain = options.bargain || false;
      this.getAddressList(true)
    } else {
      toLogin()
    }
  },
  onShow: function () {
    let that = this
    that.getAddressList(true)
  },
  methods: {
    onLoadFun: function () {
      this.getAddressList()
    },
    // 授权关闭
    authColse: function (e) {
      this.isShowAuth = e
    },
    /*
     * 导入微信地址（小程序）
     */
    getWxAddress: function () {
      let that = this
      uni.authorize({
        scope: 'scope.address',
        success: function (res) {
          uni.chooseAddress({
            success: function (res) {
              let addressP = {}
              addressP.province = res.provinceName
              addressP.city = res.cityName
              addressP.district = res.countyName
              addressP.cityId = 0
              editAddress({
                address: addressP,
                isDefault: true,
                realName: res.userName,
                postCode: res.postalCode,
                phone: res.telNumber,
                detail: res.detailInfo,
                id: 0,
                //type: 1//区别城市id（导入微信地址无城市id需要后台自己查找）;
              })
                .then((res) => {
                  that.$util.Tips(
                    {
                      title: '添加成功',
                      icon: 'success',
                    },
                    function () {
                      that.getAddressList(true)
                    }
                  )
                })
                .catch((err) => {
                  return that.$util.Tips({
                    title: err,
                  })
                })
            },
            fail: function (res) {
              if (res.errMsg == 'chooseAddress:cancel')
                return that.$util.Tips({
                  title: '取消选择',
                })
            },
          })
        },
        fail: function (res) {
          uni.showModal({
            title: '您已拒绝导入微信地址权限',
            content: '是否进入权限管理，调整授权？',
            success(res) {
              if (res.confirm) {
                uni.openSetting({
                  success: function (res) {
                    console.log(res.authSetting)
                  },
                })
              } else if (res.cancel) {
                return that.$util.Tips({
                  title: '已取消！',
                })
              }
            },
          })
        },
      })
    },
    /*
     * 导入微信地址（公众号）
     */
    getAddress() {
      let that = this
      that.$wechat.openAddress().then((userInfo) => {
        // open();
        editAddress({
          realName: userInfo.userName,
          phone: userInfo.telNumber,
          address: {
            province: userInfo.provinceName,
            city: userInfo.cityName,
            district: userInfo.countryName,
            cityId: 0,
          },
          detail: userInfo.detailInfo,
          postCode: userInfo.postalCode,
          isDefault: true,
        })
          .then(() => {
            that.$util.Tips(
              {
                title: '添加成功',
                icon: 'success',
              },
              function () {
                // close();
                that.getAddressList(true)
              }
            )
          })
          .catch((err) => {
            // close();
            return that.$util.Tips({
              title: err || '添加失败',
            })
          })
      })
    },
    /**
     * 获取地址列表
     *
     */
    getAddressList: function (isPage) {
   
      let that = this
      if (isPage) {
        that.loadend = false
        that.page = 1
        that.$set(that, 'addressList', [])
      }
      if (that.loading) return
      if (that.loadend) return
      that.loading = true
      that.loadTitle = ''
      getAddressList({
        page: that.page,
        limit: that.limit,
      })
        .then((res) => {
          let list = res.data.list
          let loadend = list.length < that.limit
          that.addressList = that.$util.SplitArray(list, that.addressList)
          that.$set(that, 'addressList', that.addressList)
          that.loadend = loadend
          that.loadTitle = loadend ? '我也是有底线的' : '加载更多'
          that.page = that.page + 1
          that.loading = false
        })
        .catch((err) => {
          that.loading = false
          that.loadTitle = '加载更多'
        })
    },
    /**
     * 设置默认地址
     */
    radioChange: function (e) {
      let index = parseInt(e.detail.value),
        that = this
      let address = this.addressList[index]
      if (address == undefined)
        return that.$util.Tips({
          title: '您设置的默认地址不存在!',
        })
      setAddressDefault(address.id)
        .then((res) => {
          for (let i = 0, len = that.addressList.length; i < len; i++) {
            if (i == index) that.addressList[i].isDefault = true
            else that.addressList[i].isDefault = false
          }
          that.$util.Tips(
            {
              title: '设置成功',
              icon: 'success',
            },
            function () {
              that.$set(that, 'addressList', that.addressList)
            }
          )
        })
        .catch((err) => {
          return that.$util.Tips({
            title: err,
          })
        })
    },
    setCopy(item) {
			let that = this
      uni.setClipboardData({
        data:
          item.realName +
          ' ' +
          item.phone +
          ' ' +
          item.province +
          item.city +
          item.district +
          item.detail,
        success: function () {
          that.$util.Tips(
            {
              title: '复制成功',
              icon: 'success',
            },
            function () {}
          )
        },
      })
    },
    /**
     * 编辑地址
     */
    editAddress: function (id) {
      let cartId = this.cartId,
        pinkId = this.pinkId,
        couponId = this.couponId
      this.cartId = ''
      this.pinkId = ''
      this.couponId = ''
      uni.navigateTo({
        url:
          '/pages/users/user_address/index?id=' +
          id +
          '&cartId=' +
          cartId +
          '&pinkId=' +
          pinkId +
          '&couponId=' +
          couponId +
          '&secKill' +
          this.secKill +
          '&combination=' +
          this.combination +
          '&bargain=' +
          this.bargain+
          '&orderType='+this.orderType+
          '&pickupCode='+this.pickupCode,
      })
    },
    /**
     * 删除地址
     */
    delAddress: function (index) {
      let that = this,
        address = this.addressList[index]
      if (address == undefined)
        return that.$util.Tips({
          title: '您删除的地址不存在!',
        })
      delAddress(address.id)
        .then((res) => {
          that.$util.Tips(
            {
              title: '删除成功',
              icon: 'success',
            },
            function () {
              that.addressList.splice(index, 1)
              that.$set(that, 'addressList', that.addressList)
            }
          )
        })
        .catch((err) => {
          return that.$util.Tips({
            title: err,
          })
        })
    },
    /**
     * 新增地址
     */
    addAddress: function () {
      let cartId = this.cartId,
        pinkId = this.pinkId,
        couponId = this.couponId
      this.cartId = ''
      this.pinkId = ''
      this.couponId = ''
      uni.navigateTo({
        url: '/pages/users/user_address/index?preOrderNo=' + this.preOrderNo+
          '&orderType='+this.orderType+
          '&pickupCode='+this.pickupCode,
      })
    },
    goOrder: function (id) {
      if (this.preOrderNo) {
        if(this.orderType=='order'){

     
        uni.redirectTo({
          url:
            '/pages/users/order_confirm/index?is_address=1&preOrderNo=' +
            this.preOrderNo +
            '&addressId=' +
            id,
        })   }
        else{
          uni.redirectTo({
          url:
            '/pages/users/order_confirm/gift_index?is_address=1&preOrderNo=' +
            this.preOrderNo +
            '&addressId=' +
            id+
          '&pickupCode='+this.pickupCode,
        }) 
        }
      }
    },
  },
  onReachBottom: function () {
    this.getAddressList()
  },
}
</script>

<style lang="scss" scoped>
.address-management {
  // padding: 20rpx 30rpx;
}
.address-management.fff {
  background-color: #fff;
  height: 1300rpx;
}

.line {
  width: 100%;
  height: 3rpx;
  image {
    width: 100%;
    height: 100%;
    display: block;
  }
}
.address-management .item {
  background-color: #fff;
  padding: 0 20rpx;
  margin-bottom: 20rpx;
}

.address-management .item .address {
  padding: 30rpx 0;
  border-bottom: 1rpx solid #eee;
  font-size: 28rpx;
  color: #666;
}

.address-management .item .address .consignee {
  margin-bottom: 10rpx;

  font-size: 30rpx;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #333333;
  line-height: 42rpx;
}

.address-management .item .address .consignee .phone {
  margin-left: 25rpx;
  font-size: 28rpx;
}

.address-management .item .operation {
  height: 83rpx;
  font-size: 26rpx;
  color: #999999;
}

.address-management .item .operation .radio text {
  margin-left: 13rpx;
}

.address-management .item .operation {
  text-align: center;
  .iconfont {
    color: #333333;
    font-size: 26rpx;
    vertical-align: -2rpx;
    margin-right: 50rpx;
  }
  .iconfont:last-child {
    margin-right: 0;
  }
}

.address-management .item .operation .iconfont.icon-shanchu {
  margin-left: 35rpx;
  font-size: 38rpx;
}

.footer {
  position: fixed;
  width: 100%;
  background-color: #fff;
  bottom: 0;
  height: 106rpx;
  padding: 0 30rpx;
  box-sizing: border-box;
}

.footer .addressBnt {
  width: 330rpx;
  height: 76rpx;

  text-align: center;
  line-height: 76rpx;
  font-size: 30rpx;
  color: #fff;
  background: $bg-gradient;
  border-radius: 36rpx;
}

.footer .addressBnt.on {
  width: 690rpx;
  margin: 0 auto;
}

.footer .addressBnt .iconfont {
  font-size: 35rpx;
  margin-right: 8rpx;
  vertical-align: -1rpx;
}

.footer .addressBnt.wxbnt {
  background-color: #fe960f;
}
/deep/ uni-radio .uni-radio-input.uni-radio-input-checked {
  background: $bg-gradient;
  border: none !important;
}
.checked-text {
  color: #ff6816;
}
</style>
